import { create } from "zustand";
import { chat } from "@/apis";
import { getIDBase } from "@/utils";
interface ChatStore {
    currentChatInfo: any; // 当前聊天信息
    currentConversation: any; // 当前对话
    messageList: any[]; // 会话聊天消息列表
    chatHistoryList: any[]; // 历史会话列表
    chatHistoryObject: any; // 历史记录对象
    answerCreating: boolean; // 回答创建中
    createNewChatAction: (payload: any) => void; // 创建新聊天
    updateAnswerCreatingAction: (flag: boolean) => void; // 更新回答创建状态
    getAllChatHistoryAction: () => Promise<void>; // 获取全部历史记录
    deleteChatHistoryFetchAction: () => Promise<void>; // 删除历史记录
    getConversationListFetchAction: () => Promise<void>; // 获取对话列表
}

export const useChatStore = create<ChatStore>((set,get) => ({
    currentChatInfo: {},
    chatHistoryList:[],
    messageList: [],
    answerCreating: false,
    currentConversation: {},
    // 创建新聊天
    createNewChatAction: (payload: any) => {
        const newUserMessage = {
            role: 'user',
            message: payload.message,
            sendType: payload.sendType,
            id: getIDBase()
        }
        const newAssistantMessage = {
            role: 'assistant',
            question: payload.message,
            message:'',
            isNewSend: true,
            sendType: payload.sendType,
            id: getIDBase()
        }
        set((state) => ({
            messageList: [...state.messageList, newUserMessage, newAssistantMessage]
        }))
        // 是否是新对话聊天
     
    },

    // 更新回答创建状态
    updateAnswerCreatingAction: (payload: boolean) => {
        set((state) => ({
            answerCreating: payload
        }))
    },

    // 更新消息列表
    updateMessageListAction: (flag: boolean) => {
        set((state) => ({
            messageList: flag
        }))
    },

    // 获取全部历史记录
    getAllChatHistoryAction: (conversation: any) => {
        return new Promise((resolve, reject) => {
            const payload = {
                conversationId: conversation.id
            }
            chat.getChatHistoryFetch(payload).then(res => {
                set((state) => ({
                    messageList: res.data.map((item: any) => ({
                        ...item,
                        sendType: item.type,
                        role: item.role,
                        message: item.content,
                        id: item.id
                    }))
                }))
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },

    // 删除历史记录
    deleteChatHistoryFetchAction: () => {
        chat.deleteChatHistoryFetch().then(res => {
            console.log(res)
        })
    },

    // 获取对话列表
    getConversationListFetchAction: () => {
       return new Promise((resolve, reject) => {
        chat.getConversationListFetch().then(res => {
            set((state) => ({
                chatHistoryList: res.data.map(item => {
                    return {
                        ...item,
                        loading: false
                    }
                })
            }))
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
       })
    },

    // 更新当前对话
    updateCurrentConversationAction: (payload: any) => {
        set((state) => ({
            currentConversation: payload
        }))
    },

    // 清空消息列表
    clearMessageListAction: () => {
        set((state) => ({
            messageList: []
        }))
    },
     
    //清除当前对话
    clearCurrentConversationAction: () => {
        set((state) => ({
            currentConversation: {}
        }))
    },
  
    //删除对话
    deleteConversationAction: (payload: { conversationId: number }) => {
      return new Promise((resolve, reject) => {
          chat.deleteConversationFetch(payload).then(res => {
          const newArr = get().chatHistoryList.filter((item: any) => item.id !== payload.conversationId)
          set((state) => ({
            chatHistoryList: newArr
          }))
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },

    //创建新对话
    createNewConversationAction: () => {
        set((state) => ({
            currentConversation: {},
            messageList:[]
        }))
    },

    //添加一个新的会话
    addNewConversationAction: (payload: any) => {
        set((state) => ({
            chatHistoryList: [payload, ...state.chatHistoryList]
        }))
    },

    //更新会话loading状态
    updateConversationLoadingOkAction: () => {
        set((state) => ({
            chatHistoryList: state.chatHistoryList.map((item: any) => ({...item, loading: false}))
        }))
    }
}));
